'use strict';

const Controller = require('egg').Controller;
const BaseController = require('./base');
const { loginRule, passwordRule, createRule, editRule } = require('../validator/admin')
const moment = require("moment");
const { success, commonErr, md5 } = require('../extend/helper');


class AdminController extends BaseController {

    /**
     * 登录
     */
    async login() {
        const data = this.ctx.request.body;
        const error = this.app.validator.validate(loginRule, data);
        error && commonErr({beans: error});
        const admin = await this.service.admin.findByUsername(data.username);
        if (!admin) {
            commonErr('用户不存在');
        }
        if (md5(md5(data.password)) != admin.password) {
            commonErr('密码错误');
        }
        const token = await this.service.token.getToken(admin.id);
        success({bean: token});
    }

    /**
     * 修改密码
     */
    async password() {
        const data = this.ctx.request.body;
        const error = this.app.validator.validate(passwordRule, data);
        error && commonErr({beans: error});
        if (data.password != data.confirm_password) {
            commonErr('输入密码不一致');
        }
        const id = await this.userId();
        const admin = await this.ctx.model.Admin.findByPk(id);
        if (!admin) {
            commonErr('用户不存在');
        }
        admin.password = data.password;
        admin.save();
        success();
    }

    /**
     * 管理员列表
     */
    async list() {
        const page = parseInt(this.ctx.query.page) ? parseInt(this.ctx.query.page) : 0;
        const size = parseInt(this.ctx.query.size) ? parseInt(this.ctx.query.size) : 15;
        const {rows, count} = await this.ctx.model.Admin.findAndCountAll({
            offset: page * size,
            limit: size
        });
        success({beans: {
            rows, count, page, size

        }})
    }

    /**
     * 新增
     */
    async add() {
        const data = this.ctx.request.body;
        const error = this.app.validator.validate(createRule, data);
        error && commonErr({beans: error});
        if (data.password != data.confirm_password) {
            commonErr('输入密码不一致');
        }
        const admin = await this.service.admin.findByUsername(data.username);
        if (admin) {
            commonErr(`用户${admin.username}已存在`);
        }
        data.password = md5(md5(data.password));
        console.log(data)
        const res = await this.ctx.model.Admin.create(data);
        success();
    }

    /**
     * 编辑
     */
    async edit() {
        const data = this.ctx.request.body;
        const error = this.app.validator.validate(editRule, data);
        error && commonErr({beans: error});
        if (data.password != data.confirm_password) {
            commonErr('输入密码不一致');
        }
        const id = this.ctx.params.id;
        const admin = await this.ctx.model.Admin.findByPk(id);
        if (!admin) {
            commonErr(`用户不存在`);
        }
        admin.nickname = data.nickname;
        admin.password = md5(md5(data.password));
        admin.save();
        success();
    }

    /**
     * 删除
     */
    async delete() {
        const id = this.ctx.params.id;
        const admin = await this.ctx.model.Admin.findByPk(id);
        if (!admin) {
            commonErr(`用户不存在`);
        }
        if (admin.username == 'admin') {
            commonErr(`不能删除超级管理员admin`);
        }
        admin.destroy();
        success();
    }

}

module.exports = AdminController;